package tp.pr5.gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;

import tp.pr5.Directions;
import tp.pr5.MapObserver;
import tp.pr5.Room;
import tp.pr5.RoomInfo;

import java.awt.Cursor;

public class MapPanel extends JPanel implements MapObserver
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MapCell[][] _mapcellArray=new MapCell[11][11];
	private JPanel _mapa;
	private JTextArea _roomTextField;
	private MapCell _currentMapCell;
	public MapPanel() {
		setLayout(new BorderLayout(0, 0));
		
		_roomTextField = new JTextArea();
		_roomTextField.setBorder(new TitledBorder(null, "Room", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		_roomTextField.setEditable(false);
		add(_roomTextField, BorderLayout.SOUTH);
		
		
		_mapa = new JPanel(new GridLayout(11, 11));
		_mapa.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		_mapa.setBorder(new TitledBorder(null, "Map", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		
		for (int i=0;i<11;i++){
            for (int j=0;j<11;j++) {
            	this._mapcellArray[i][j]=new MapCell(j,i,this._roomTextField);
                _mapa.add(this._mapcellArray[i][j]);
            }
        }
		//inicializamos el primer cuarto para currentMapCell
		this._currentMapCell=this.searchMapCell(5, 5);
		add(_mapa, BorderLayout.CENTER);
	}
	public MapCell searchMapCell(int x, int y) {
		
		for (int i=0;i<11;i++){
         for (int j=0;j<11;j++) {
         	if (this._mapcellArray[i][j].get_x()==x && this._mapcellArray[i][j].get_y()==y)
         		return this._mapcellArray[i][j];
         }
     }
		return null;
	}
	@Override
	public void playerHasExaminedRoom(RoomInfo targetRoom) {
		this._roomTextField.setText(targetRoom.getDescription());
		
	}
	@Override
	public void roomEntered(Directions direction, RoomInfo targetRoom) {
	
		
		
		//por si es el primero
		if (direction!=null) {
		//	JOptionPane.showMessageDialog(null, "...moving to " + direction);
			this._currentMapCell.setBackground(Color.GRAY);
		
		switch (direction)
		{

		case NORTH :
			this._currentMapCell = this.searchMapCell(
					this._currentMapCell.get_x(), this._currentMapCell.get_y() - 1);
			break;
		case SOUTH :
			this._currentMapCell = this.searchMapCell(
					this._currentMapCell.get_x(), this._currentMapCell.get_y() + 1);
			break;
		case  EAST:
			this._currentMapCell = this.searchMapCell(
					this._currentMapCell.get_x() + 1, this._currentMapCell.get_y());
			break;
		case WEST:
			this._currentMapCell = this.searchMapCell(
					this._currentMapCell.get_x() - 1, this._currentMapCell.get_y());
			break;
		}
		}
		this.enterMapCell(targetRoom);
		
	}
	public MapCell get_currentMapCell()
	{
		return _currentMapCell;
	}
	public void set_currentMapCell(MapCell _currentMapCell)
	{
		this._currentMapCell = _currentMapCell;
	}
	private void enterMapCell(RoomInfo targetRoom) {
		
		this._currentMapCell.set_habitacion(targetRoom);
		this._currentMapCell.setBackground(Color.GREEN);
		this._currentMapCell.setText(targetRoom.getName());
	}
}